﻿/*+===================================================================
File:      BossTwoLeftPartArm.cpp

Summary:   Định nghĩa các phương thức của CBossTwoLeftPartArm.
===================================================================+*/

#include "BossTwoRightPartArm.h"


CBossTwoRightPartArm::CBossTwoRightPartArm(CBill* bill, D3DXVECTOR2 pos, int id, int typeId)
	: CBossTwoPartArm(bill, pos, id, typeId)
{
	m_fAngle = 135.0f;
	direction = 1;
}


CBossTwoRightPartArm::~CBossTwoRightPartArm()
{
}

void CBossTwoRightPartArm::Update(float timeFrame)
{
	if (!m_bIsLive)
		return;

	// Bắn đạn theo thời gian
	if (m_nStatus == STATUS_UP_DOWN || m_nStatus == STATUS_END_ROTATE || m_nStatus == STATUS_WING)
	{
		if (m_timeFire > 0)
		{
			m_timeFire -= timeFrame;
		}
		else
		{
			m_timeFire = 0.0f;
			Fire();
		}
	}

	switch (m_nStatus)
	{
	case STATUS_START:
	{
		if (m_nId != 0 && m_bVisible)
		{
			m_fR += 70.0f * timeFrame;
			if (m_fR >= R)
			{
				m_fR = R;
				prevPartArm->SetVisible(true);
			}
		}

		if (m_nId == 0 && m_bVisible)
		{
			ChangeStatus(STATUS_UP_DOWN);
			CBossTwoPartArm* nextArm = nextPartArm;
			while (nextArm != NULL)
			{
				if (nextArm->GetId() == 1)
					nextArm->angleVec = MAX_VEC;
				else
					nextArm->angleVec = 0.0f;
				nextArm->ChangeStatus(STATUS_UP_DOWN);
				nextArm = nextArm->nextPartArm;
			}
		}

		break;
	}
	case STATUS_UP_DOWN:
	{
		if (m_nId == 1)
		{
			// Chặn dưới.
			if (m_fAngle < 0 && m_fAngle >= -60 && direction == 1)
			{
				direction = -1;
				m_nCount++;

				CBossTwoPartArm* nextArm = nextPartArm;
				while (nextArm != NULL)
				{
					nextArm->angleVec = MAX_VEC / 2.0f;
					nextArm = nextArm->nextPartArm;
				}
			}

			//// Chặn trên.
			if (m_fAngle > 0 && m_fAngle <= 60 && direction == -1)
			{
				direction = 1;

				CBossTwoPartArm* nextArm = nextPartArm;
				while (nextArm != NULL)
				{
					nextArm->angleVec = MAX_VEC / 2.0f;
					nextArm = nextArm->nextPartArm;
				}
			}

			if (m_nCount > 2)
			{
				ChangeStatus(STATUS_ROTATE);
				CBossTwoPartArm* nextArm = nextPartArm;
				while (nextArm != NULL)
				{
					nextArm->ChangeStatus(STATUS_ROTATE);
					nextArm = nextArm->nextPartArm;
				}
			}

		}

		break;
	}
	case STATUS_ROTATE:
	{
		if (m_nId != 0)
		{
			if (m_fAngle > 0 && m_fAngle < 45 && direction == -1)
			{
				if (m_nCount > 0)
				{
					if (!m_checkCount)
					{
						m_fAngle = 45;
						angleVec = 0;
						ChangeStatus(STATUS_END_ROTATE);
						if (m_nId == 4)
							Fire();
					}
				}
				else
				{
					if (!m_checkCount)
					{
						m_nCount++;
						m_checkCount = true;
					}
				}
			}
			else if (m_fAngle > 0 && m_fAngle > 45 && direction == -1)
			{
				m_checkCount = false;
			}
		}

		break;
	}
	case STATUS_END_ROTATE:
	{
		if (m_nId == 1)
		{
			if (m_timeChange > 0.0f)
			{
				m_timeChange -= timeFrame;
			}
			else
			{
				ChangeStatus(STATUS_WING);
				direction = 1;
				angleVec = MAX_VEC;

				CBossTwoPartArm* nextArm = nextPartArm;
				while (nextArm != NULL)
				{
					nextArm->angleVec = MAX_VEC / 2.0f;
					nextArm->direction = 1;
					nextArm->ChangeStatus(STATUS_WING);
					nextArm = nextArm->nextPartArm;
				}
			}
		}

		break;
	}
	case STATUS_WING:
	{
		if (direction == 1)
		{
			if (m_nId == 1)
			{
				if (m_fAngle > 135)
				{
					angleVec = 0.0f;
				}
			}
			else if (m_nId != 0)
			{
				float angle = m_fAngle;
				float preAngle = prevPartArm->m_fAngle;
				if (angle < 0)
					angle = angle + 360;

				if (preAngle < 0)
					preAngle = preAngle + 360;

				float distance = angle - preAngle;

				if (distance > 35)
				{
					angleVec = 0.0f;
				}
			}
		}

		if (m_timeChange > 0.0f)
		{
			m_timeChange -= timeFrame;
		}
		else
		{
			ChangeStatus(STATUS_FOLLOW);
		}

		break;
	}
	case STATUS_FOLLOW:
	{
		if (m_timeChange > 0.0f)
		{

			m_timeChange -= timeFrame;

			D3DXVECTOR2 billPos = m_bill->GetPos();
			float angle = atan2(billPos.y - m_pos.y, billPos.x - m_pos.x) * 180 / PI;

			float angleMin = angle - 7.5f;
			float angleMax = angle + 7.5f;

			if (angleMax * m_fAngle > 0)
			{
				if (angleMax < m_fAngle)
				{
					angleVec = MAX_VEC;
					direction = -1;
				}
				else if (angleMin > m_fAngle)
				{
					angleVec = MAX_VEC;
					direction = 1;
				}
				else if (angleMin < m_fAngle && m_fAngle < angleMax)
				{
					angleVec = 0.0f;
				}
			}
			else
			{
				angleVec = MAX_VEC;
				direction = 1;
			}

		}
		else
		{
			m_timeChange = 0.0f;

			if (m_nId == 1)
			{
				direction = -1;
				angleVec = MAX_VEC;
				ChangeStatus(STATUS_UP_DOWN);

				CBossTwoPartArm* nextArm = nextPartArm;
				while (nextArm != NULL)
				{
					nextArm->angleVec = MAX_VEC / 2.0f;

					nextArm->ChangeStatus(STATUS_UP_DOWN);
					nextArm = nextArm->nextPartArm;
				}
			}
		}

		break;
	}
	}

	if (prevPartArm != NULL)
	{

		D3DXVECTOR2 prePos = prevPartArm->GetPos();
		m_fAngle += angleVec * timeFrame * direction;

		if (m_fAngle > 180)
			m_fAngle -= 360;
		else if (m_fAngle < -180)
			m_fAngle = 360 - abs(m_fAngle);

		m_pos.x = prePos.x + m_fR * cos(m_fAngle * PI / 180.0f);
		m_pos.y = prePos.y + m_fR * sin(m_fAngle * PI / 180.0f);
	}

	// Cập nhật lại vận tốc max khi góc hiện tại cách góc phía sau hơn 45 độ.
	if (nextPartArm != NULL && m_nId != 0)
	{
		float distance = abs(m_fAngle - nextPartArm->m_fAngle);
		if (distance > 180)
			distance = 360 - distance;

		if (distance > 45)
		{
			nextPartArm->angleVec = MAX_VEC;
			nextPartArm->direction = direction;
		}
	}

	UpdateBox(timeFrame);
}